function toTree(data) {
    let result = []
    if (!Array.isArray(data)) {
        return result
    }
    data.forEach(item => {
        delete item.children;
    });
    let map = {};
    data.forEach(item => {
        map[item.id] = item;
    });
    data.forEach(item => {
        let parent = map[item.pid];
        if (parent) {
            (parent.children || (parent.children = [])).push(item);
        } else {
            result.push(item);
        }
    });
    return result;
}

let depts = { "depts":[{"id":"11","pid":"2"},{"id":"12","pid":"11"},{"id":"13","pid":"11"},{"id":"14","pid":"12"},{"id":"15","pid":"6"},{"id":"16","pid":"7"}]};
console.log(depts);

depts.depts=toTree(depts.depts);

console.log(depts);